package com.blogSystem.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.blogSystem.user.dao.AdminDao;
import com.blogSystem.user.pojo.Admin;
import com.blogSystem.user.pojo.User;
import com.blogSystem.user.service.AdminService;
import com.blogSystem.user.service.SuperService;
import com.blogSystem.user.service.UserService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import util.IdWorker;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/super")
public class SuperController {
    final public static int SIZE=10;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private SuperService superService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpSession session;
    @Autowired
    private UserService userService;
    @Autowired
    private IdWorker idWorker;


    @RequestMapping(value = "/add",method = RequestMethod.PUT)
    public Result add(@RequestBody User user){
        if(superService.searchAdmin(user)!=null)
        {
            return new Result(false, StatusCode.HAS, "管理员已存在");
        }



        superService.searchAdmin(user);

        return new Result(true, StatusCode.OK, "添加成功");
    }
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public Result delete(@RequestBody String id){

        superService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }
    @RequestMapping(value = "/one",method = RequestMethod.POST)
    public Result add(@RequestBody String id){

        User user=superService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功",user);
    }
    @RequestMapping(value = "/list/{page}",method = RequestMethod.POST)
    public Result list(@PathVariable int page){

        System.out.println("page:"+page);
        Page<User> pages=superService.findAll(page,SIZE);
        return new Result(true, StatusCode.OK, "查询成功",pages);
    }
    @RequestMapping(value = "/search/{page}",method = RequestMethod.POST)
    public Result search(@RequestBody User user,@PathVariable int page){


        Page<User> pages=superService.findSearch(user,page,SIZE)        ;
        return new Result(true, StatusCode.OK, "查询成功",pages);
    }
    @RequestMapping(value = "/changename",method = RequestMethod.PUT)
    public Result changename(@RequestBody User user){

        if (superService.changeName(user.getId(),user)) {
            return new Result(true, StatusCode.OK, "修改成功");
        }
        else
            return new Result(false, StatusCode.HAS, "用户名已存在");
    }
    @RequestMapping(value = "/changepass",method = RequestMethod.PUT)
    public Result changepass(@RequestBody Map map){

        String id= (String) map.get("id");
        LinkedHashMap linkedHashMap = (LinkedHashMap) map.get("admin");
        String json=new JSONObject().toJSONString(linkedHashMap);
       User newAdmin= JSONObject.parseObject(json,User.class);

        if (superService.changePass(id,newAdmin)) {
            return new Result(true, StatusCode.OK, "修改成功");
        }
        else
            return new Result(false, StatusCode.ERROR, "修改失败");
    }
    @RequestMapping(value = "/deletelist",method = RequestMethod.DELETE)
    public Result deletelist(@RequestBody List<String> ids){
       userService.deleteList(ids);
        return new Result(true, StatusCode.OK, "删除成功");

    }

}
